/*
 * 官网地站:http://www.mob.com
 * 技术支持QQ: 4006852216
 * 官方微信:ShareSDK   （如果发布新版本的话，我们将会第一时间通过微信将版本更新内容推送给您。如果使用过程中有任何问题，也可以通过微信与我们取得联系，我们将会在24小时内给予回复）
 *
 * Copyright (c) 2013年 mob.com. All rights reserved.
 */

package com.masterclient.app.wxapi;

import android.app.Activity;
import android.os.Bundle;

import com.eoner.baselib.utils.activity.ActivityHook;
import com.eoner.middlelib.eventbus.EventBusManager;
import com.eoner.middlelib.eventbus.enums.WXSendTypeEnum;
import com.eoner.middlelib.eventbus.enums.WXSendTypeManager;
import com.eoner.middlelib.eventbus.event.WXSendCancelEvent;
import com.eoner.middlelib.eventbus.event.WXSendFailedEvent;
import com.eoner.middlelib.eventbus.event.WXSendSuccessEvent;
import com.eoner.managerlibrary.weixin.WeiXinManager;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

	private IWXAPI api;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		ActivityHook.hookOrientation(this);//hook，绕过检查
		super.onCreate(savedInstanceState);
		try {
			api = WXAPIFactory.createWXAPI(this,  WeiXinManager.APP_ID);
			api.handleIntent(getIntent(), this);
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onReq(BaseReq baseReq) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onResp(BaseResp baseResp) {
		if (baseResp == null) {
			finish();
			return;
		}
		switch (baseResp.errCode) {
			case BaseResp.ErrCode.ERR_OK: {
				//TODO:成功
				if(baseResp.getType() == 1) {
					//1表示登录
					try{
						SendAuth.Resp sendResp = (SendAuth.Resp) baseResp;
						String code = sendResp.code;
						WXSendSuccessEvent wxLoginOrShareEvent = new WXSendSuccessEvent(WXSendTypeManager.getInstance().getWxType());
						wxLoginOrShareEvent.setCode(code);
						EventBusManager.post(wxLoginOrShareEvent);
					} catch (Exception e){
						finish();
					}
				} else if(baseResp.getType() == 2){
					//2表示分享
					WXSendSuccessEvent wxLoginOrShareEvent = new WXSendSuccessEvent(WXSendTypeEnum.SHARE_EVENT_TYPE);
					EventBusManager.post(wxLoginOrShareEvent);
				}
				finish();
			}
				break;
			case BaseResp.ErrCode.ERR_USER_CANCEL: {
				//TODO:取消
				if(baseResp.getType() == 1) {
					WXSendCancelEvent wxSendCancelEvent = new WXSendCancelEvent(WXSendTypeManager.getInstance().getWxType());
					EventBusManager.post(wxSendCancelEvent);
				} else if (baseResp.getType() == 2) {
					//登录
					WXSendCancelEvent wxSendCancelEvent = new WXSendCancelEvent(WXSendTypeEnum.SHARE_EVENT_TYPE);
					EventBusManager.post(wxSendCancelEvent);
				}
				finish();
				break;
			}
			default: {
				//TODO:失败
				if(baseResp.getType() == 1){
					WXSendFailedEvent wxSendFailedEvent = new WXSendFailedEvent(WXSendTypeManager.getInstance().getWxType());
					EventBusManager.post(wxSendFailedEvent);
				} else if (baseResp.getType() == 2){
					WXSendFailedEvent wxSendFailedEvent = new WXSendFailedEvent(WXSendTypeEnum.SHARE_EVENT_TYPE);
					EventBusManager.post(wxSendFailedEvent);
				}
				finish();
				break;
			}
		}
	}
}
